package com.mlan.userap.component;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


import com.mlan.userap.R;
import com.mlan.userap.activity.ChatActivity;

import com.mlan.userap.widget.EmojiIndicatorView;

import java.util.ArrayList;
import java.util.List;



public class LoadFace {
    private final Context context;
    ViewPager faceViewPager;
    EmojiIndicatorView faceIndicator;


    ArrayList<View> ViewPagerItems = new ArrayList<>();
    private EmojiClickListener mItemEventListener = null;
    public LoadFace(Context ctx) {
        context = ctx;
    }

    public static LoadFace newInstance(Context context,ViewPager pager,EmojiIndicatorView faceIn) {
        LoadFace fragment = new LoadFace(context);
        fragment.faceViewPager = pager;
        fragment.faceIndicator = faceIn;
        //FaceManager.loadEmojis();
        return fragment;
    }

    public void Show(){
        List<FaceGroup> faceGroups = FaceManager.getFaceGroupList();
        if( faceGroups.size()>0){
            final FaceGroup group = faceGroups.get(0);
            ArrayList<Emoji> faces = group.getFaces();
            initViewPager(faces, group.getPageColumnCount(), group.getPageRowCount());

        }
    }

    private void initViewPager(ArrayList<? extends Emoji> list, int columns, int rows) {
        intiIndicator(list, columns, rows);
        ViewPagerItems.clear();
        int pageCont = getPagerCount(list, columns, rows);
        for (int i = 0; i < pageCont; i++) {
            ViewPagerItems.add(getViewPagerItem(i, list, columns, rows));
        }
        FaceVPAdapter mVpAdapter = new FaceVPAdapter(ViewPagerItems);
        faceViewPager.setAdapter(mVpAdapter);
        faceViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int oldPosition = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                faceIndicator.playBy(oldPosition, position);
                oldPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void intiIndicator(ArrayList<? extends Emoji> list, int columns, int rows) {
        faceIndicator.init(getPagerCount(list, columns, rows));
    }
    private int getPagerCount(ArrayList<? extends Emoji> list, int columns, int rows) {
        int count = list.size();
        int dit = 0;
        return count % (columns * rows - dit) == 0 ? count / (columns * rows - dit)
                : count / (columns * rows - dit) + 1;
    }

    class FaceVPAdapter extends PagerAdapter {
        private List<View> views;

        public FaceVPAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) (arg2));
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(views.get(arg1));
            return views.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }
    }
    static class FaceGVAdapter extends BaseAdapter {
        private final List<Emoji> list;
        private final Context mContext;

        public FaceGVAdapter(List<Emoji> list, Context mContext) {
            super();
            this.list = list;
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            FaceGVAdapter.ViewHolder holder;
            Emoji chatFace = list.get(position);
            if (convertView == null) {
                holder = new FaceGVAdapter.ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_face, null);
                holder.iv = convertView.findViewById(R.id.face_image);
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.iv.getLayoutParams();
                if (chatFace != null && chatFace.getHeight() != 0 && chatFace.getWidth() != 0) {
                    params.width = chatFace.getWidth();
                    params.height = chatFace.getHeight();
                }
                holder.iv.setLayoutParams(params);
                convertView.setTag(holder);
            } else {
                holder = (FaceGVAdapter.ViewHolder) convertView.getTag();
            }

            if (chatFace != null) {
                FaceManager.loadFace(chatFace, holder.iv);
            }
            return convertView;
        }

        class ViewHolder {
            ImageView iv;
        }
    }

    private View getViewPagerItem(int position, ArrayList<? extends Emoji> list, int columns, int rows) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.layout_face_grid, null);
        GridView gridview = layout.findViewById(R.id.chart_face_gv);
        gridview.setNumColumns(columns);
        /**
         * 注：因为每一页末尾都有一个删除图标，所以每一页的实际表情columns *　rows　－　1; 空出最后一个位置给删除图标
         *
         * Because there is a delete icon at the end of each page, the actual emoji of each page columns *　rows　－　1, Empty the last position for the delete icon
         * */
        final List<Emoji> subList = new ArrayList<>();
        int dit  = 0;
        subList.addAll(list.subList(position * (columns * rows - dit), (columns * rows - dit) * (position + 1) > list .size() ? list.size() : (columns * rows - dit) * (position + 1)));


        FaceGVAdapter mGvAdapter = new FaceGVAdapter(subList, context);
        gridview.setAdapter(mGvAdapter);
        gridview.setNumColumns(columns);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Emoji emoji = (Emoji) subList.get(position);
                if(mItemEventListener!=null) mItemEventListener.onEmojiClick(emoji);

            }
        });

        return gridview;
    }

    public void setEmojiClick(EmojiClickListener mItemEventListener) {
        this.mItemEventListener = mItemEventListener;
    }

}
